﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Technoera.Common.Interop.OleDb
{
    public class ParseEnumImportMapping : IImportMapping
    {
        public ParseEnumImportMapping(string sourceColumnName, PropertyInfo targetPropertyInfo, Type enumType, Dictionary<string, object> enumTextValues)
        {
            SourceColumnName = sourceColumnName;
            TargetPropertyInfo = targetPropertyInfo;
            EnumType = enumType;
            EnumTextValues = enumTextValues;
        }

        private string _sourceColumnName;
        public string SourceColumnName
        {
            get
            {
                if (_sourceColumnName == null)
                    _sourceColumnName = string.Empty;

                return _sourceColumnName;
            }
            set
            {
                _sourceColumnName = value;
            }
        }

        public PropertyInfo TargetPropertyInfo { get; set; }

        public Type EnumType { get; set; }

        private Dictionary<string, object> _enumTextValues;
        public Dictionary<string, object> EnumTextValues
        {
            get
            {
                if (_enumTextValues == null)
                    _enumTextValues = new Dictionary<string, object>();

                return _enumTextValues;
            }
            private set
            {
                _enumTextValues = value;
            }
        }
	

        #region IImportMapping Members
        public void SetPropertyValue(System.Data.IDataReader dataReader, object instance)
        {
            object sourceValue = dataReader[SourceColumnName];
            if (sourceValue is System.DBNull) return;
            string sourceString = (string)sourceValue;
            if (EnumTextValues.ContainsKey(sourceString))
            {
                TargetPropertyInfo.SetValue(instance, EnumTextValues[sourceString], null);
            }
        }
        #endregion

    }
}
